package com.dgz.springboot2302.controller;

import com.dgz.springboot2302.entity.Article;
import com.dgz.springboot2302.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author duguozheng
 * @Date 2023/03/17  18:01
 * @Description 写文章的那个类
 */
@Controller
public class ArticleController {

    private static File articleDir;

    static {
        articleDir = new File("./articles");
        if (!articleDir.exists()) {
            articleDir.mkdirs();
        }
    }

    @RequestMapping("/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("注意！用户准备发表文章了！！！！！！！！！！");
        //获取表单信息
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println("表单信息：\n" + "title:" + title + "  author:" + author + "  content:" + content);

        if (title == null || title.isEmpty() ||
                content == null || content.isEmpty() ||
                author == null || author.isEmpty()) {
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        Article article = new Article(title, author, content);
        File file = new File(articleDir, title + ".obj");
        if (file.exists()) {
            try {
                response.sendRedirect("/article_exists.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
            oos.writeObject(article);
            response.sendRedirect("/article_success.html");//注册成功后跳转指定页面
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/articleList")
    public void articleList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理显示文章列表");
        List<Article> articleList = new ArrayList<>();//创建集合来保存users目录下所有用户信息

        //获取users目录下所有.obj文件
        File[] files = articleDir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(".obj");
            }
        });

        //遍历file数组中每一个obj文件，反序列化并存入userList集合
        if (files != null) {
            for (File f : files) {
                try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
                    Article article = (Article) ois.readObject();
                    articleList.add(article);
                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();

            //写页面
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>文章列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>用户列表</h1>");
            pw.println("<table border='3'>");

            pw.println("<tr>");
            pw.println("<td>文章名</td>");
            pw.println("<td>坐作者</td>");
            pw.println("</tr>");

            for (Article article : articleList) {
                pw.println("<tr>");
                pw.println("<td>" + article.getTitle() + "</td>");
                pw.println("<td>" + article.getAuthor() + "</td>");
                pw.println("</tr>");
            }

            pw.println("</table>");
            pw.println("<h2><a href=\"/index.html\">返回首页</a></h2>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
